/*
 * @Author: wangdazhuang
 * @Date: 2024-10-17 17:36:57
 * @LastEditTime: 2024-10-17 22:12:44
 * @LastEditors: wangdazhuang
 * @Description: 
 * @FilePath: /pornhub_app/lib/src/views/mine/profit/profit_header.dart
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_utils/src/extensions/widget_extensions.dart';
import 'package:pornhub_app/assets/styles.dart';
import 'package:pornhub_app/services/user_service.dart';
import 'package:pornhub_app/utils/extension.dart';
import '../../../http/api/api.dart';
import '../../../model/mine/profit_total_model.dart';
import '../../../routes/routes.dart';
import '../../../utils/color.dart';

class ProfitHeader extends StatefulWidget {
  const ProfitHeader({super.key});
  @override
  State<StatefulWidget> createState() {
    return ProfitHeaderState();
  }
}

class ProfitHeaderState extends State<ProfitHeader> {
  ProfitTotalModel _model = ProfitTotalModel.fromJson({});
  Future getData() async {
    final rsp = await Api.fecthSpreadTotalData();
    if (rsp != null) {
      _model = rsp;
      if (mounted) {
        setState(() {});
      }
    }
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  _buildItem(String t, double v) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          t,
          style: kTextStyle(Colors.white, fontsize: 14.w),
        ),
        SizedBox(height: 5.w),
        Text(
          "$v",
          style: kTextStyle(Colors.white, fontsize: 20.w),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final num = Get.find<UserService>().user.inviteUserNum ?? 0;
    return Container(
      height: 124.w,
      width: 368.w,
      decoration: BoxDecoration(
          gradient: LinearGradient(
              colors: [COLOR.hexColor("#0066F2"), COLOR.hexColor("#00E6EE")]),
          borderRadius: BorderRadius.circular(4.w)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(
            width: 300.w,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                _buildItem("今日收益（金币）", _model.todayIncome ?? 0),
                Container(width: 1.w, height: 50.w, color: Colors.white),
                _buildItem("累计收益（金币）", _model.todayIncome ?? 0),
              ],
            ),
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "享${(_model.promRatio ?? 0) * 100}%好友消费返现",
                style: kTextStyle(Colors.white, fontsize: 14.w),
              ),
              Text(
                "好友人数：$num人 >",
                style: kTextStyle(Colors.white, fontsize: 12.w),
              ),
              Image.asset("assets/mine/invite_btn.png",
                      width: 60.w, height: 21.w)
                  .onTap(() {
                Get.toShare();
              }),
            ],
          ).marginOnly(top: 25.w).marginSymmetric(horizontal: 30.w),
        ],
      ),
    ).marginOnly(top: 13.w);
  }
}
